package systems.jMenuBuilder.elements.menus;

import java.util.ArrayList;

import javax.swing.JMenu;

import systems.jMenuBuilder.elements.Elements;
import systems.jMenuBuilder.elements.items.CheckBoxMenuItem;
import systems.jMenuBuilder.elements.items.Items;
import systems.jMenuBuilder.elements.items.MenuItem;
import systems.jMenuBuilder.elements.items.RadioButtonMenuItem;
import systems.languages.MultiLanguages;

public class Menu extends JMenu implements Elements{

  private static final long serialVersionUID = 1L;

  private String nameEntryKey;
  private String tooltipEntryKey;
  private char mnemonicKey;
  private ArrayList<Menu> subMenuList;
  private ArrayList<Items> itemsList;

  public Menu()
  {
    super();
  }

  public Menu(String nameEntryKey, String tooltipEntryKey, char mnemonicKey)
  {
    super();
    /*
    System.out.println("\t->Menu : NameEntry="+nameEntryKey+
        "\n\t\ttooltipEntry="+tooltipEntryKey+
        "\n\t\tmnemonicKey="+mnemonicKey);
    */
    this.subMenuList = new ArrayList<Menu>();
    this.itemsList = new ArrayList<Items>();
    //Save the generals data relative to the menu.
    this.nameEntryKey = nameEntryKey;
    this.tooltipEntryKey = tooltipEntryKey;
    this.mnemonicKey = mnemonicKey;
    //DEBUG TRACE
    /*System.out.println("Creating a menu. "+"\n"+
        "Name (entry): "+this.nameEntryKey+" -> "+MultiLanguages.getProperties(this.nameEntryKey)+"\n"+
        "Tooltip (entry)"+tooltipEntryKey+"\n"+
        "Mnemonic "+mnemonicKey);*/
    //END OF TRACE
    super.setText(MultiLanguages.getProperties(this.nameEntryKey));
    
    if(this.tooltipEntryKey != null)
      super.setToolTipText(MultiLanguages.getProperties(this.nameEntryKey));
    
    super.setMnemonic(this.mnemonicKey);
  }

  public void addItem(MenuItem menuItem) {
    super.add(menuItem);
    //System.out.println("\t->Ajout de l'item : NameEntry="+menuItem.getText());
    this.itemsList.add(menuItem);
  }

  public void addItem(CheckBoxMenuItem checkBoxMenuItem) {
    //System.out.println("\t->Ajout de la checkbox item : NameEntry="+checkBoxMenuItem.getText());
    super.add(checkBoxMenuItem);
    this.itemsList.add(checkBoxMenuItem);
  }

  public void addItem(RadioButtonMenuItem radioButtonMenuItem) { 
    //System.out.println("\t->Ajout du radiobutton item : NameEntry="+radioButtonMenuItem.getText());
    super.add(radioButtonMenuItem);
    this.itemsList.add(radioButtonMenuItem);
  }

  public void addSubMenu(Menu subMenu) {
    //System.out.println("\t->Ajout du sous menu : NameEntry="+subMenu.getText());
    super.add(subMenu);
    this.subMenuList.add(subMenu);
  }
  
  public ArrayList<Items> getAllItems()
  {
    return this.itemsList;
  }
  
  public Items getItemsNamed(String itemName)
  {
    for(Items i: this.itemsList)
      if( ((MenuItem)i).getText().equalsIgnoreCase(itemName) )
        return i;
    return null;
  }
  
  public ArrayList<Menu> getAllSubmenu()
  {
    return this.subMenuList;
  }
  
  public Menu getSubMenuNamed(String subMenuName)
  {
    for(Menu m: this.subMenuList)
      if( m.getText().equalsIgnoreCase(subMenuName) )
        return m;
    return null;
  }
  
  public String toString()
  {
    String toString = "\t->Menu : NameEntry="+nameEntryKey+
    "\n\t\ttooltipEntry="+tooltipEntryKey+
    "\n\t\tmnemonicKey="+mnemonicKey;
    toString.concat("\n\t\t[");
    for(Menu menu: this.subMenuList)
      toString.concat(menu.toString());
    toString.concat("]");
    return toString;
  }

  @Override
  public void update() {
    super.setText(MultiLanguages.getProperties(this.nameEntryKey));
    if(this.tooltipEntryKey != null)
      super.setToolTipText(MultiLanguages.getProperties(this.nameEntryKey));
    //Update all the subMenu
    for (Menu subMenu : subMenuList)
      subMenu.update();
    //Update all the items
    for (Items item : itemsList)
      item.update();
  }
}
